package com.rxt.thermopro.ui.fragment

import android.os.Bundle
import androidx.core.view.isVisible
import androidx.lifecycle.ViewModelProvider
import com.app.common.base.BaseFragment
import com.app.common.viewmodel.BaseViewModel
import com.rxt.thermopro.R
import com.rxt.thermopro.app.LocalDataDao
import com.rxt.thermopro.app.LocalDataDao.Companion.TEMP_UNIT_C
import com.rxt.thermopro.bean.ProfileBean
import com.rxt.thermopro.ui.dialog.TypeBottomDialog.Companion.TYPE_RANGE_TEMP
import com.rxt.thermopro.viewmodel.EventViewModel
import com.rxt.thermopro.widget.InputFilterMinMax
import kotlinx.android.synthetic.main.fragment_create_profile.*
import kotlinx.android.synthetic.main.fragment_create_profile.backButton
import kotlinx.android.synthetic.main.fragment_create_profile.viewSave
import kotlinx.android.synthetic.main.fragment_edit_profile.*

class CreateProfileFragment : BaseFragment<BaseViewModel>() {
    override fun layoutId(): Int {
        return R.layout.fragment_create_profile
    }

    companion object {
        private const val ARG_SECTION_TYPE = "type"

        fun newInstance(sectionType: Int): CreateProfileFragment {
            return CreateProfileFragment().apply {
                arguments = Bundle().apply {
                    putInt(ARG_SECTION_TYPE, sectionType)
                }
            }
        }
    }

    private var currentType = -1
    private lateinit var mEventViewModel: EventViewModel

    override fun initView(savedInstanceState: Bundle?) {
        mEventViewModel =
            ViewModelProvider(requireActivity()).get(EventViewModel::class.java)
        currentType = arguments?.getInt(ARG_SECTION_TYPE, -1) ?: -1
        if (currentType == TYPE_RANGE_TEMP) {
            targetTempNum.isVisible = false
            viewTargetUnit.isVisible = false
            lineView.isVisible = true
            viewHigh.isVisible = true
            lineView1.isVisible = true
            viewLow.isVisible = true
        } else {
            targetTempNum.isVisible = true
            viewTargetUnit.isVisible = true
            lineView.isVisible = false
            viewHigh.isVisible = false
            lineView1.isVisible = false
            viewLow.isVisible = false
        }
        viewTargetUnit.text = LocalDataDao.INSTANCE.getTempUnit()
        viewHighestUnit.text = LocalDataDao.INSTANCE.getTempUnit()
        viewLowestUnit.text = LocalDataDao.INSTANCE.getTempUnit()
        if (LocalDataDao.INSTANCE.getTempUnit() == TEMP_UNIT_C) {
            targetTempNum.filters = arrayOf(InputFilterMinMax(0, 99))
            viewHighestNum.filters = arrayOf(InputFilterMinMax(0, 99))
            viewLowestNum.filters = arrayOf(InputFilterMinMax(0, 99))
        } else {
            targetTempNum.filters = arrayOf(InputFilterMinMax(0, 211))
            viewHighestNum.filters = arrayOf(InputFilterMinMax(0, 211))
            viewLowestNum.filters = arrayOf(InputFilterMinMax(0, 211))
        }
        setListener()
    }

    private fun setListener() {
        backButton.setOnClickListener {
            requireActivity().onBackPressed()
        }

        viewDiscard.setOnClickListener {
            requireActivity().onBackPressed()
        }

        viewSave.setOnClickListener {
            val profileBean = if (currentType == TYPE_RANGE_TEMP) {
                if (LocalDataDao.INSTANCE.getTempUnit() == LocalDataDao.TEMP_UNIT_C) {
                    val highTempF =
                        LocalDataDao.INSTANCE.transUnitCToF(viewHighestNum.text.toString())
                    val lowTempF =
                        LocalDataDao.INSTANCE.transUnitCToF(viewLowestNum.text.toString())
                    ProfileBean(
                        viewProfileName.text.toString(), viewLowestNum.text.toString() + "~"
                                + viewHighestNum.text.toString(), mutableListOf(),
                        highTemperatureNum = viewHighestNum.text.toString(),
                        lowTemperatureNum = viewLowestNum.text.toString(),
                        highValueF = highTempF,
                        lowValueF = lowTempF, singleTemp = false,
                        iconRes = R.drawable.meat9_setting, isCustomAdd = true
                    )
                } else {
                    val highTemp =
                        LocalDataDao.INSTANCE.transUnitFToC(viewHighestNum.text.toString())
                    val lowTemp = LocalDataDao.INSTANCE.transUnitFToC(viewLowestNum.text.toString())
                    ProfileBean(
                        viewProfileName.text.toString(), viewLowestNum.text.toString() + "~"
                                + viewHighestNum.text.toString(), mutableListOf(),
                        highTemperatureNum = highTemp, lowTemperatureNum = lowTemp,
                        highValueF = viewHighestNum.text.toString(),
                        lowValueF = viewLowestNum.text.toString(), singleTemp = false,
                        iconRes = R.drawable.meat9_setting, isCustomAdd = true
                    )
                }
            } else {
                if (LocalDataDao.INSTANCE.getTempUnit() == LocalDataDao.TEMP_UNIT_C) {
                    val highTempF =
                        LocalDataDao.INSTANCE.transUnitCToF(targetTempNum.text.toString())
                    ProfileBean(
                        viewProfileName.text.toString(), targetTempNum.text.toString(),
                        mutableListOf(), highTemperatureNum = targetTempNum.text.toString(),
                        highValueF = highTempF,
                        iconRes = R.drawable.meat9_setting, isCustomAdd = true
                    )
                } else {
                    val highTemp =
                        LocalDataDao.INSTANCE.transUnitFToC(targetTempNum.text.toString())
                    ProfileBean(
                        viewProfileName.text.toString(), targetTempNum.text.toString(),
                        mutableListOf(), highTemperatureNum = highTemp,
                        highValueF = targetTempNum.text.toString(),
                        iconRes = R.drawable.meat9_setting, isCustomAdd = true
                    )
                }
            }
            LocalDataDao.INSTANCE.addProfile(profileBean)
            mEventViewModel.updateProfile()
            requireActivity().onBackPressed()
        }
    }
}